package com.sctvcloud.dongqu.ui.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;

import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.beans.FChannel;
import com.sctvcloud.dongqu.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.dongqu.ui.fragment.ProgramBackFragment;
import com.sctvcloud.dongqu.ui.fragment.RadioFragment;
import com.sctvcloud.dongqu.ui.fragment.TvLivingFragment;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/7/13 0013.
 */

public class TvAdapter extends FragmentPagerAdapter {
    private TvLivingFragment tvLivingFragment;
    private boolean isParentShowing;
    private ArrayList<FChannel> channels;
    private SparseArray<WeakReference<ColumnBaseFragment>> fragmentWeaks;

    public TvAdapter(FragmentManager fm, Context context, ArrayList<FChannel> channels) {
        super(fm);
        this.channels = channels;
        fragmentWeaks = new SparseArray<>();
    }

    @Override
    public Fragment getItem(int position) {
        tvLivingFragment = new TvLivingFragment();
        tvLivingFragment.setChannelUrl(channels.get(position).getLiveProgrammeList());
        tvLivingFragment.setIndexInViewPager(position);
        fragmentWeaks.put(position, new WeakReference<ColumnBaseFragment>(tvLivingFragment));
        return tvLivingFragment;

    }

    public ColumnBaseFragment getFragmentByPos(int pos) {
        WeakReference<ColumnBaseFragment> wrf = fragmentWeaks.get(pos);
        if (wrf != null) {
            return wrf.get();
        }
        return null;
    }

    @Override
    public int getCount() {
        return channels.size();
    }

    public void setData(ArrayList<FChannel> channels) {
        this.channels = channels;
        notifyDataSetChanged();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        //  return super.getPageTitle(position);
        return channels.get(position).getChannelName();
    }

    public void setParentShowing(boolean parentShowing) {
        isParentShowing = parentShowing;
//        if (tvLivingFragment != null) {
//            tvLivingFragment.setParentShowing(isParentShowing);
//        }

    }


    public TvLivingFragment getTvLivingFragment() {
        return tvLivingFragment;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

}
